﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary.RoomModule
{
    public class RoomManagement
    {
        private const string ErrEmptyVal = "ERROR: Empty value";
        private const string ErrInvalidData = "ERROR: Invalid data";
        private const string Error = "ERROR: ";
        private readonly RoomDAO _roomDao = new RoomDAO();

        public string AddEquipment(string name, decimal price, bool available)
        {
            try
            {
                if (name.Trim() != "")
                {

                    var equipmentDetail = new EquipmentDetail
                                              {
                                                  Available = available,
                                                  EquipmentPrice = price,
                                                  EquipmentName = name
                                              };
                    return _roomDao.InsertEquipment(equipmentDetail);

                }
                return ErrEmptyVal;
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public List<EquipmentDetail> GetEquipment(bool available)
        {
            return available ? _roomDao.SelectEquipment(1) : _roomDao.SelectEquipment(-1);
        }

        public EquipmentDetail GetEquipmentByID(int id)
        {
            return _roomDao.SelectEquipmentByID(id);
        }

        public List<EquipmentDetail> GetEquipmentByName(string name)
        {
            return _roomDao.SearchEquipmentByName(name);
        }

        public string ModifyEquipment(int id, string name, decimal price, bool available)
        {
            try
            {
                if (name.Trim() != "")
                {
                    var equipmentDetail = new EquipmentDetail
                                              {
                                                  EquipmentDetailID = id,
                                                  Available = available,
                                                  EquipmentName = name,
                                                  EquipmentPrice = price
                                              };
                    return _roomDao.UpdateEquipment(equipmentDetail);
                }
                return ErrEmptyVal;
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public RoomType GetRoomTypeByID(int id)
        {
            return _roomDao.SelectRoomTypeByID(id);
        }

        public List<RoomType> GetRoomType()
        {
            return _roomDao.SelectRoomType();
        }

        public List<EquipmentDetail> GetEquipmentByRoomTypeID(int id)
        {

            RoomType roomType = _roomDao.SelectRoomTypeByID(id);
            return _roomDao.SelectEquipment(roomType);

        }

        public string AddRoomType(string title, string rank, decimal price, bool bath, bool view, bool balcon,
                                  List<int> equipmentDetailID)
        {
            try
            {
                if (title.Trim() != "")
                {
                    var equipmentDetails = new List<EquipmentDetail>();
                    foreach (var id in equipmentDetailID)
                    {
                        equipmentDetails.Add(_roomDao.SelectEquipmentByID(id));
                    }
                    var roomType = new RoomType
                                       {
                                           RoomTypeTitle = title,
                                           RoomRank = rank,
                                           RoomPrice = price,
                                           Balcony = balcon,
                                           Bath = bath,
                                           View = view
                                       };
                    return _roomDao.InsertRoomType(roomType, equipmentDetails);
                }
                return ErrEmptyVal;
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public string ModifyRoomType(int id, string title, string rank, decimal price, bool bath, bool view,
                                     bool balcony, List<int> equipmentDetailId)
        {
            try
            {
                if (title.Trim() != "" && rank.Trim() != "")
                {

                    var equipmentDetails = new List<EquipmentDetail>();
                    foreach (var eq in equipmentDetailId)
                    {
                        equipmentDetails.Add(GetEquipmentByID(eq));
                    }
                    var roomType = new RoomType
                                       {
                                           RoomTypeID = id,
                                           RoomTypeTitle = title,
                                           RoomRank = rank,
                                           RoomPrice = price,
                                           Bath = bath,
                                           View = view,
                                           Balcony = balcony
                                       };
                    return _roomDao.UpdateRoomType(roomType, equipmentDetails);

                }
                return ErrEmptyVal;
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public List<Room> GetRoom(int available)
        {
            
                    return _roomDao.SelectRoom(available);
             
        }

        public string AddRoom(int roomId, int status, int roomTypeId)
        {
            try
            {

                if (status > 1 || status < -1)
                {
                    return ErrInvalidData;
                }
                var room = new Room
                               {
                                   RoomID = roomId,
                                   Status = status,
                                   RoomTypeID = roomTypeId
                               };
                return _roomDao.InsertRoom(room);

            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public string ModifyRoom(int roomId, int status, int roomTypeId)
        {
            try
            {

                if (status > 1 || status < -1)
                {
                    return ErrInvalidData;
                }
                var room = new Room
                               {
                                   RoomID = roomId,
                                   Status = status,
                                   RoomTypeID = roomTypeId
                               };
                return _roomDao.UpdateRoom(room);

            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public Room GetRoomByID(int id)
        {
            return _roomDao.SelectRoomByID(id);
        }

        public List<Room> GetRoomByRoomTypeID(int roomTypeId)
        {
            return _roomDao.SelectRoomByRoomTypeID(roomTypeId);
        }
    }
}